package com.example.a2105_ivideo.test_mvi

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.a2105_ivideo.databinding.ActivityTestMviBinding
import com.example.lib_mvi.base.BaseMVIFragment
import kotlinx.coroutines.launch

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/24
 */
class TestMviFragment:BaseMVIFragment() {
    private lateinit var viewModel:MyViewModel
    val binding: ActivityTestMviBinding by lazy {
        ActivityTestMviBinding.inflate(layoutInflater)
    }
    override fun generateViewModel() {
        viewModel = ViewModelProvider(this)[MyViewModel::class.java]
    }
    //懒加载数据
    override fun lazyLoad() {
        lifecycleScope.launch {
            viewModel.intents.send(MyIntent.GetBannerData)
        }
    }


    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return binding.root
    }

    override fun handleState() {
       lifecycleScope.launch {
           repeatOnLifecycle(Lifecycle.State.STARTED){
               viewModel.bannerDataStateFlow_.collect{
                   when(it){
                       is MyState.Success -> {

                       }
                       is MyState.Error -> {

                       }
                       is MyState.Loading -> {

                       }
                       is MyState.Empty -> {

                       }
                   }
               }
           }
       }
    }
}